package com.hk.service.quartz;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.hk.commons.utils.MsgTypeConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hk.commons.CacheConstants;
import com.hk.commons.ConstantPush;
import com.hk.commons.JsonUtils;
import com.hk.commons.RedisCilent;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.message.PaymentMsg;
import com.hk.service.IPush;
import com.hk.service.sys.ISysUser;

public class PaymentPushQuartz extends BaseQuartz {
	private Logger logger = LoggerFactory.getLogger(PaymentPushQuartz.class);
	@Autowired
	public ISysUser user;
	@Autowired
	private IPush pushService;

	public void sendToFinancial() {
		try {
			List<String> list = RedisCilent.listGet(
					CacheConstants.FINALCIAL_PUSH, 0, 10);
			for (String str : list) {
				PaymentMsg payment = JsonUtils.toBean(str, PaymentMsg.class);
				SysUser userInfo = user.findById(payment.getUserId());
				if (userInfo != null) {
					String recommendCode = userInfo.getRecommendCode();
					if (StringHelper.isNotEmpty(recommendCode)) {
						String custName = payment.getAccountName();
						boolean pushResult = sendPushToFinancial(custName,
								recommendCode);
						//if (pushResult) {
						RedisCilent.delListVal(CacheConstants.FINALCIAL_PUSH, 1,
								str);
						//}
					}
				}
			}
		} catch (Exception e) {
			logger.error("绑卡推送给理财师异常！error:", e);
		}

	}

	private boolean sendPushToFinancial(String custName, String phone) {
		boolean result = false;
		// 您的客户张四，已成功在华财通绑卡
		if (StringHelper.isNotEmpty(custName) && StringHelper.isNotEmpty(phone)) {
			String msg = ConstantPush.PUSH_BINDCARD_START + custName
					+ ConstantPush.PUSH_BINDCARD_END;
			result = pushService.jgPushMsg(msg, null, null, null, phone,
					ConstantPush.PUSH_MENU_BINDCARD, MsgTypeConstants.BIND_CARD);
		}
		return result;
	}

	@Override
	public void start() {
		ExecutorService executor = Executors.newFixedThreadPool(1);
        Runnable task = new Runnable() {
            public void run() {
            	sendToFinancial();
            }
        };
        executor.execute(task);
        executor.shutdown();
	}
}
